from random import random
from time import strftime, sleep
from turtle import title, bgcolor, setup, tracer, Pen

# 设置标题
title("七段数码管绘制")
# 设置画布背景色
bgcolor("#000")
# 设置窗口大小和位置
setup(1000, 200, 200, 200)
# 设置跳过动画
tracer(2)
yearPen = monthPen = dayPen = hourPen = minutePen = secondPen = Pen()


# 获取颜色
def getColor():
    R = random()
    G = random()
    B = random()
    return R, G, B


# 画笔初始化
def turtleInit():
    newPen = Pen()
    newPen.hideturtle()
    newPen.pensize(3)
    newPen.speed(0)
    return newPen


# 画笔移动
def penMove(dist, pen):
    pen.penup()
    pen.fd(dist)
    pen.pendown()


# 画线，true表示画实线，false表示移动
def drawLine(draw, pen):
    # 在绘制开始和结束加一个间隔
    penMove(3, pen)
    pen.pendown() if draw else pen.penup()
    # 向前15px
    pen.fd(15)
    # 加一个间隔
    penMove(3, pen)
    # 右转90°
    pen.right(90)


# 根据数字绘制七段数码管
def drawDigit(digit, pen):
    drawLine(True, pen) if digit in [2, 3, 4, 5, 6, 8, 9] else drawLine(False, pen)
    drawLine(True, pen) if digit in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawLine(False, pen)
    drawLine(True, pen) if digit in [0, 2, 3, 5, 6, 8, 9] else drawLine(False, pen)
    drawLine(True, pen) if digit in [0, 2, 6, 8] else drawLine(False, pen)
    pen.left(90)
    drawLine(True, pen) if digit in [0, 4, 5, 6, 8, 9] else drawLine(False, pen)
    drawLine(True, pen) if digit in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False, pen)
    drawLine(True, pen) if digit in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False, pen)
    pen.left(180)
    penMove(20, pen)


# 绘制年
def drawYear():
    global yearPen
    yearPen.hideturtle()
    # 获取年
    year = strftime("%Y")
    # 清空画布
    yearPen.clear()
    # 绘制年的turtle对象
    yearPen = turtleInit()
    penMove(-480, yearPen)
    # 画笔颜色
    yearPen.pencolor("#f00")
    for i in year:
        drawDigit(eval(i), yearPen)


# 绘制月
def drawMonth():
    global monthPen
    monthPen.hideturtle()
    # 获取月
    month = strftime("%m")
    # 清空画布
    monthPen.clear()
    # 绘制月的turtle对象
    monthPen = turtleInit()
    penMove(-250, monthPen)
    monthPen.pencolor("#0f0")
    for i in month:
        drawDigit(eval(i), monthPen)
    drawYear()


# 绘制日
def drawDayOfMonth():
    global dayPen
    dayPen.hideturtle()
    # 获取日
    dayOfMonth = strftime("%d")
    # 清空画布
    dayPen.clear()
    # 绘制日的turtle对象
    dayPen = turtleInit()
    penMove(-100, dayPen)
    dayPen.pencolor("#00f")
    for i in dayOfMonth:
        drawDigit(eval(i), dayPen)
    drawMonth()


# 绘制时
def drawHour():
    global hourPen
    hourPen.hideturtle()
    # 获取时
    hour = strftime("%H")
    if hour == "00":
        drawDayOfMonth()
    # 清空画布
    hourPen.clear()
    # 绘制时的turtle对象
    hourPen = turtleInit()
    penMove(50, hourPen)
    hourPen.pencolor(getColor())
    for i in hour:
        drawDigit(eval(i), hourPen)


# 绘制分
def drawMinute():
    global minutePen
    minutePen.hideturtle()
    # 获取分
    minute = strftime("%M")
    if minute == "00":
        drawHour()
    # 清空画布
    minutePen.clear()
    # 绘制分的turtle对象
    minutePen = turtleInit()
    penMove(200, minutePen)
    minutePen.pencolor(getColor())
    for i in minute:
        drawDigit(eval(i), minutePen)


# 绘制秒
def drawSecond():
    global secondPen
    secondPen.hideturtle()
    # 获取秒
    second = strftime("%S")
    if second == "00":
        drawMinute()
    # 清空画布
    secondPen.clear()
    # 绘制秒的turtle对象
    secondPen = turtleInit()
    penMove(350, secondPen)
    secondPen.pencolor(getColor())
    for i in second:
        drawDigit(eval(i), secondPen)


# 绘制文字
def drawWord():
    word = ["年", "月", "日", "时", "分", "秒"]
    # 绘制文字的turtle对象
    wordPen = turtleInit()
    # 设置初始位置
    penMove(-300, wordPen)
    wordPen.right(90)
    penMove(10, wordPen)
    wordPen.left(90)
    name = "Arial"
    size = 15
    family = "normal"
    dist = 150
    for i in range(len(word)):
        wordPen.pencolor("#fff")
        wordPen.write(word[i], font=(name, size, family))
        penMove(dist, wordPen)


# 主函数
def main():
    # 绘制文字
    drawWord()
    drawSecond()
    drawMinute()
    drawHour()
    drawDayOfMonth()
    while True:
        drawSecond()
        sleep(1)


try:
    main()
finally:
    exit(0)
